package com.example.xt.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.xt.entity.Orders;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

/**
 * <p>
 * Mapper 接口
 * </p>
 *
 * @author ${author}
 * @since 2022-03-05
 */
@Mapper
public interface OrdersMapper extends BaseMapper<Orders> {
    @Insert("INSERT into `orders` (price)  VALUES('0')")
//自动生成订单表的id和time
    boolean save0();

    @Update("UPDATE rder JOIN orders set rder.medicine=orders.medicine WHERE orders.time=rder.time AND rder.medicine is NULL")
//将对应的订单编号从订单传递到订单详情
    boolean save1();

    @Update("UPDATE rder  set prices=price*num")
    boolean updata0();

    @Update("UPDATE rder SET rder.profit =( SELECT price - costprice FROM medicine WHERE medicine.id = rder.id )*num WHERE medicine=(SELECT MAX(medicine) )")
//仅计算最新数据
//计算单个物品的总价与利润
    boolean updata1();

    @Update("UPDATE orders set orders.price=(SELECT SUM(prices) FROM rder WHERE rder.medicine=orders.medicine)")
    boolean updata2();

    @Update("UPDATE orders set orders.profit=(SELECT SUM(profit) FROM rder WHERE rder.medicine=orders.medicine)")
//将订单总价汇总到订单表
    boolean updata3();

    @Select("SELECT SUM(price) FROM orders where rent is null and DATEDIFF(time,NOW())=-#{i}")
//查询某天销售总额
    Integer sel(int i);

    @Select("SELECT SUM(profit) FROM orders where rent is null and DATEDIFF(time,NOW())=-#{i}")
//查询某天利润总额
    Integer selprofit(int i);

    @Update("UPDATE medicine set stock=stock-#{num} WHERE id=#{id}")
//减少对应库存
    void del(Integer id, String num);


}
